package com.es.code.annotation.aspect;

import com.es.code.annotation.LogAround;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author jichf
 * @Description
 * @create 2021-11-04 15:49
 */

@Component
@Aspect
public class LogAroundAspectAdvice {
    @Around("com.es.code.annotation.architecture.Architecture.anyMethod() && @annotation(logAround)")
    public Object logAround(ProceedingJoinPoint jp, LogAround logAround) throws Throwable {
        Logger log = LoggerFactory.getLogger(jp.getTarget().getClass());
        String methodName = jp.getSignature().getName();
        String value = logAround.value();
        if (log.isInfoEnabled())
            log.info("进入方法：" + methodName + "()" + "----" + value);
        //执行前时间
        long start = System.currentTimeMillis();
        Object o = jp.proceed();
        //执行结束
        long end = System.currentTimeMillis();
        if (log.isInfoEnabled())
            log.info("离开方法：" + methodName + "()" + "----" + value + ", 用时：" + (end - start) + "毫秒");

        return o;
    }
}
